package com.test.exoplayertest.player.view;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.test.exoplayertest.R;
import com.test.exoplayertest.player.utils.PlayerUtil;


/**
 * @author wuxiaotao on 2017/6/19.
 */

public class PlayerFloatView extends FrameLayout {
    public static String NO_NET_ERROR_MSG = "网络未连接,请检查网络后重试。";
    public static String SOURCE_ERROR_MSG = "视频流解析出错";
    private View loadingView, errorView, retryButton;
    private TextView errorMsgTV;
    private LayoutParams params;
    private View player_volume_view;
    private ImageView player_volume_icon;
    private ImageView player_brightness_icon;
    private TextView player_volume_textview;
    private TextView player_brightness_textview;
    private View player_brightness_view;
    private View player_progress_view;
    private TextView progress_time;
    private SeekBar progress_seekbar;


    public PlayerFloatView(@NonNull Context context) {
        this(context, null);
    }

    public PlayerFloatView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PlayerFloatView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
    }

    public void showLoadingView() {
        if (null == loadingView) {
            LayoutInflater.from(getContext()).inflate(R.layout.player_float_loading_view, this);
            loadingView = findViewById(R.id.loadingView);
        } else {
            if (loadingView.getParent() == null) {
                addView(loadingView, params);
            }
        }
        hideOtherView(loadingView);
    }

    public void showErrorView(String errorMsg, OnClickListener listener) {
        if (null == errorView) {
            LayoutInflater.from(getContext()).inflate(R.layout.player_error_view, this);
            errorView = findViewById(R.id.errorView);
            errorMsgTV = (TextView) errorView.findViewById(R.id.errorMsgTV);
            retryButton = errorView.findViewById(R.id.retryButton);
        } else {
            if (errorView.getParent() == null) {
                addView(errorView, params);
            }
        }
        hideOtherView(errorView);
        errorMsgTV.setText(errorMsg);
        retryButton.setOnClickListener(listener);
    }

    public void showVolumeView(int value, int max) {
        if (null == player_volume_view) {
            LayoutInflater.from(getContext()).inflate(R.layout.player_float_volume_volume, this);
            player_volume_view = findViewById(R.id.player_volume_view);
            player_volume_icon = (ImageView) player_volume_view.findViewById(R.id.player_volume_icon);
            player_volume_textview = (TextView) player_volume_view.findViewById(R.id.player_volume_textview);
        } else {
            if (player_volume_view.getParent() == null) {
                addView(player_volume_view, params);
            }
        }
        player_volume_textview.setText(PlayerUtil.getPercent(value, max));
        if (value >= max) {
            player_volume_icon.setImageResource(R.mipmap.volume_gesture_3);
        } else if (value >= max / 2.0f) {
            player_volume_icon.setImageResource(R.mipmap.volume_gesture_2);
        } else if (value > 0) {
            player_volume_icon.setImageResource(R.mipmap.volume_gesture_1);
        } else {
            player_volume_icon.setImageResource(R.mipmap.volume_gesture_0);
        }
        hideOtherView(player_volume_view);
    }

    public void showBrightness(int value, int max) {
        if (null == player_brightness_view) {
            LayoutInflater.from(getContext()).inflate(R.layout.player_float_brightness_view, this);
            player_brightness_view = findViewById(R.id.player_brightness_view);
            player_brightness_icon = (ImageView) player_brightness_view.findViewById(R.id.player_brightness_icon);
            player_brightness_textview = (TextView) player_brightness_view.findViewById(R.id.player_brightness_textview);
        } else {
            if (player_brightness_view.getParent() == null) {
                addView(player_brightness_view, params);
            }
        }
        player_brightness_textview.setText(PlayerUtil.getPercent(value, max));
        player_brightness_icon.setImageResource(R.mipmap.player_float_brightness_icon);
        hideOtherView(player_brightness_view);
    }

    private void hideOtherView(View view) {
        setVisibility(VISIBLE);
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (child != null && child == view) {
                child.setVisibility(VISIBLE);
            } else {
                child.setVisibility(GONE);
            }
        }
    }

    public void showSeekProgress(long progress, long max) {
        if (progress > max) {
            progress = max;
        }
        if (null == player_progress_view) {
            LayoutInflater.from(getContext()).inflate(R.layout.player_float_progress_view, this);
            player_progress_view = findViewById(R.id.player_progress_view);
            progress_time = (TextView) player_progress_view.findViewById(R.id.progress_time);
            progress_seekbar = (SeekBar) player_progress_view.findViewById(R.id.progress_seekbar);
        } else {
            if (player_progress_view.getParent() == null) {
                addView(player_progress_view, params);
            }
        }
        progress_time.setText(PlayerUtil.getDuration(progress));
        progress_seekbar.setMax((int) (max / 1000));
        progress_seekbar.setProgress((int) (progress / 1000));
        hideOtherView(player_progress_view);
    }

}
